<?php
/**
 * Created by PhpStorm.
 * User: 宇
 * Date: 2016/9/30
 * Time: 15:21
 * 推荐类
 */
namespace app\admin\controller;

use app\admin\model\Positions;
use app\index\model\Content;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;
use app\admin\model\PositionsData;
use app\common\controller\Index;

class Position extends Controller
{
    /*
     * 推荐位列表
     */
    public function positionList()
    {
        if(!(Session::has('username'))) {
            return 10005;
        } else {
            //获取推荐位列表
            $pos_obj = PositionsData::all();
            $pos_arr = array();
            foreach ($pos_obj as $k => $v) {
                $pos_arr[$k] = $v->toArray();
            }
            $this->assign('pos_list', $pos_arr);
            $this->view->replace([
                '__PUBLIC__'    =>  '../../static/',
                '__ROOT__'      =>  '/daogou/public/'
            ]);
            return $this->fetch('positionList');
        }
    }

    /*
     * 新增推荐位
     */
    public function positionAdd(Request $request)
    {
        if(!(Session::has('username'))) {
            return 10005;
        } else {
            if (input('?post.dosubmit')){
                //新增推荐位
                $post = $request -> post();
                //过滤
                $pos_data = array();
                foreach ($post as $k => $v){
                    $pos_data[$k] = Index::validateInput($v);
                }
                //检查是否生效
                if (!isset($pos_data['status'])){
                    $pos_data['status'] = 0;
                }
                unset($pos_data['dosubmit']);
                $excute = model('PositionsData') ->allowField(true) ->create($pos_data);
                if (!$excute){
                    //新增失败
                    $this->error('新增失败!');
                } else {
                    $this->success('新增成功!','admin/Position/positionList');
                }
            } else {
                $this->view->replace([
                    '__PUBLIC__'    =>  '../../static/',
                    '__ROOT__'      =>  '/daogou/public/'
                ]);
                return $this->fetch('positionAdd');
            }
        }
        return 0;
    }

    /*
     * 修改推荐位
     * ajax
     */
    public function positionEdit(Request $request)
    {
        //获取前台的ajax数据
        if(!(Session::has('username'))) {
            return 10005;
        } else {
            //dump($request ->post());
            //过滤数据
            $pos_data = array();
            $post = $request -> post();

            foreach ($post as $k => $v){
                $pos_data[$k] = Index::validateInput($v);
            }
            $where['pos_id'] = $pos_data['pos_id'];
            //存入数据
            $excute = model('PositionsData') -> isUpdate(true) -> save($pos_data, $where);
            if(!$excute) {
                return 0;
            } else {
                return 1;
            }
        }
    }

    /*
     * 删除推荐位
     * ajax
     */
    public function positionDel(Request $request)
    {
        if(!(Session::has('username'))) {
            return 10005;
        } else {
            //删除推荐位
            $pos_id = $request -> get('pos_id');
            $where['pos_id'] = $pos_id;

            $excute = model('PositionsData') -> where($where) -> delete();
            if(0 !== $excute){
                //删除成功
                $this->success('删除成功!');
            } else {
                $this->error('删除失败!');
            }
        }
        return 0;
    }
    /*
     * 推荐位详情
     */
    public function positionInfo(Request $request)
    {
        if(!(Session::has('username'))) {
            return 10005;
        } else {
            $pos_id = $request -> get('pos_id');
            //获取该推荐位中的商品列表
            $where['p.pos_id'] = $pos_id;
            $where['c.del'] = 0;

            $filed = 'p.id, p.content_id, p.pos_ids, p.pos_sort, pd.pos_name, c.title, h.goods_hit, h.ticket_hit, c.url, c.update_time, p.create_time';
            $pos_list = Db::name('positions')
                ->alias('p')
                ->where($where)
                ->join('positions_data pd', 'pd.pos_id = p.pos_id')
                ->join('content c', 'c.Id = p.content_id')
                ->join('hits h', 'h.goods_id = c.Id')
                ->field($filed)
                ->select();

            $this->assign('pos_list', $pos_list);
            //如果推荐位中没有数据
            $count = count($pos_list);




            $this->assign('pos_count', $count);

            $this->view->replace([
                '__PUBLIC__'    =>  '../../static/',
                '__ROOT__'      =>  '/daogou/public/'
            ]);
            if (empty($pos_list)){
                return $this->fetch('positionnoInfo');
            } else {
                $this->assign('pos_name', $pos_list[0]['pos_name']);
            }
            return $this->fetch('positionInfo');
        }
    }
    /*
     * 推荐位停用
     * ajax
     */
    public function positionEnable(Request $request)
    {
        if(!(Session::has('username'))) {
            return 10005;
        } else {
            $pos_id = $request -> post('pos_id');
            $pos_status = $request -> post('pos_status');
            //过滤
            $pos_id = Index::validateInput($pos_id);
            //检查数据是否为0|1
            $pos_status_val = array(1, 0);
            if (!in_array((int)$pos_status, $pos_status_val)){
                $pos_status = 1;
            }
            $data['status'] = (1 == $pos_status)? 0 : 1;
            //dump($status);
            //更新数据库
            $excute = PositionsData::where(['pos_id' => $pos_id]) ->  update($data);
            if (1 == $excute) {
                return 1;
            } else {
                return 0;
            }
        }
    }
    /*
     * 商品数据加入推荐位
     */
    public function goods2pos(Request $request)
    {
        if(!(Session::has('username'))) {
            return 10005;
        } else {
            $goods_id = $request -> post('goods_id');
            $pos_id = $request -> post('pos_id');
            /*dump($goods_id);
            dump($pos_id);
            exit();*/
            if (empty($goods_id) || empty($pos_id)){
                return 10006;
            }
            //过滤
            $goods_id = Index::validateInput($goods_id);
            $pos_id = Index::validateInput($pos_id);
            //操作数据
            $data['pos_id'] = $pos_id;
            $where['pos_id'] = $pos_id;
            $data['content_id'] = $goods_id;
            $data['create_time'] = time();
            //获取当前推荐位数据的排序
            $max_ids = Positions::where(['pos_id' => $pos_id]) -> max('pos_ids');
            $data['pos_ids'] = $max_ids + 1;
            //dump($data);
            $excute = Positions::create($data);
            //更新content表中数据
            unset($data);
            unset($where);

            $where['Id'] = $goods_id;
            /*//获取原先存在的数据
            $pos_es = Db::name('content') -> field('pos_id') -> where($where) -> find();
            $data['pos_id'] = $pos_id.'|'.$pos_es['pos_id'];*/
            $data['pos_id'] = $pos_id;

            $info = Content::update($data, $where);
            //$sql = Content::getLastSql();
            //dump($sql);
            if (!$excute){
                return 0;
            } else {
               return 1;
            }
        }
    }
    /*
     * 推荐位中删除商品数据
     */
    public function goodsDel(Request $request)
    {
        if(!(Session::has('username'))) {
            return 10005;
        } else {
            $id = $request -> post('id');
            $Id = $request -> post('goods_id');
            $where['id'] = Index::validateInput($id);
            //删除推荐为商品的列表
            $excute = Positions::where($where) -> delete();
            //更新商品表中的pos_id为0
            Content::where(['Id' => $Id]) -> update(['pos_id' => 0]);
            if(0 == $excute){
                return 0;
            } else {
                return 1;
            }
        }
    }
}